﻿using Liju.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LiJu.Record
{
    public partial class SettingComboxControl : UserControl
    {
        private Form frmCheckList;
        CheckedListBox checkedListBox = new CheckedListBox();
        List<string> SelectedItemList = new List<string>();
        public Action<List<string>> SelectItemEvent;
        public SettingComboxControl()
        {
            InitializeComponent();
            //TableLayoutPanelHelper.SetTableLayoutRowHeight(tbLayoutPanel_Param, 1, 0);

            //窗体
            frmCheckList = new Form();
            this.frmCheckList.Size = new Size(this.Width, 200);
            frmCheckList.FormBorderStyle = FormBorderStyle.None;
            frmCheckList.StartPosition = FormStartPosition.Manual;
            frmCheckList.BackColor = Color.White;
            frmCheckList.ShowInTaskbar = false;
            frmCheckList.Deactivate += FrmCheckList_Deactivate;
            checkedListBox.Dock = DockStyle.Fill;
            checkedListBox.ItemCheck += CheckedListBox_ItemCheck;


            //checkedListBox.
            frmCheckList.Controls.Add(checkedListBox);
        }

        private void FrmCheckList_Deactivate(object sender, EventArgs e)
        {
            frmCheckList.Hide();
            SelectedItemList.Clear();
            foreach (var item in checkedListBox.CheckedItems)
            {
                SelectedItemList.Add(item.ToString());
            }
            SelectedItemList.Remove("全部");
            SelectItemEvent?.Invoke(SelectedItemList);
        }
        private void CheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if(e.Index==0)
            {
                if(e.NewValue==CheckState.Checked)
                {
                    for (int i = 1; i < checkedListBox.Items.Count; i++)
                    {
                        checkedListBox.SetItemChecked(i, true);
                    }
                }
                else
                {
                    for (int i = 1; i < checkedListBox.Items.Count; i++)
                    {
                        checkedListBox.SetItemChecked(i, false);
                    }
                }
            }
            //var item = checkedListBox.Items[1];
            //if (checkedListBox.SelectedItem!=null &&  checkedListBox.SelectedItem.ToString()== "全部")
            //{
            //    if(checkedListBox.GetItemChecked(0))
            //    {
            //        for (int i = 1; i < checkedListBox.Items.Count; i++)
            //        {
            //            checkedListBox.SetItemChecked(i, true);
            //        }
            //    }
            //    else
            //    {
            //        for (int i = 1; i < checkedListBox.Items.Count; i++)
            //        {
            //            checkedListBox.SetItemChecked(i, false);
            //        }
            //    }

            //}

        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        public void InitData(Dictionary<string,bool> keys)
        {
            checkedListBox.Items.Add("全部", false);
            foreach (var item in keys)
            {
                checkedListBox.Items.Add(item.Key, item.Value);
            }
        }
        private void pictrue_Setting_Click(object sender, EventArgs e)
        {

            Rectangle rec = this.RectangleToScreen(this.ClientRectangle);
            this.frmCheckList.Location = new Point(rec.X, rec.Y + 40);
            this.frmCheckList.Show();
            this.frmCheckList.BringToFront();

        }

    }
}
